/*
 * Canvas Game Library
 * Module: Particle
 * 
 * Copyright Chainsawrus Wreaks 2011
 * All rights reserved.
 */

/**
 * @fileOverview A Particle Engine for CGL, here I'll stuff the particle data and emitter module and so on!
 */

/**
 * Particle module namespace
 * @namespace CGL.particle
 */
CGL.particle = {
		
	Particle : function(px, py, vx, vy, s, l) {
		particle = {
			pos : {x : px || 0, y : py || 0},
			vel : {x : vx || 0, y : vy || 0},
			ang : 0,
			angVel : 0,
			size : s || 1,
			initLife : l || 100,
			life : l || 100,
			alpha : 1.0,
			
			init : function() {

			},
			
			setVelocity : function(vx, vy) {
				this.vel.x = vx;
				this.vel.y = vy;
			},
			
			setAngle : function(angle) {
				this.ang = angle;
			},
			
			setAngleVelocity : function(aVel) {
				this.angVel = aVel;
			},
			
			setSize : function(s) {
				this.size = s;
			},
			
			setLife : function(l) {
				this.life = l;
			},
			
			getPosX : function() {
				return this.pos.x;
			},
			
			getPosY : function() {
				return this.pos.y;
			},
			
			getAngle : function() {
				return this.ang;
			},
			
			getSize : function() {
				return this.size;
			},
			
			update : function(grav, wind, growth) {
				this.pos.x += this.vel.x;
				this.pos.y += this.vel.y;
				this.vel.x += wind/100;
				this.vel.y += grav/100;
				this.size += growth;
				this.life -= 1;
				this.alpha = this.life/this.initLife;
				if(this.life < 0)
				{
					return true;
				}
			},
			
			render : function(img, ctx) {
				ctx.save();
				ctx.globalAlpha = this.alpha;
				img.renderScaled(ctx, this.pos.x, this.pos.y, this.size);
				ctx.restore();
			}
		};
		return particle;
	},
	
	Emitter : function(sx, sy, vx, vy, pps) {
		emit = {
			particles : new Array(),
			image : new CGL.image.Image(),
			pos : {x : sx || 0, y : sy || 0},
			velMax : {x : vx || 0, y : vy || 0},
			velMin : {x : vx - 0.2 || 0, y : vy - 0.2 || 0},
			initSize : {min : 1, max : 1},
			growth : 0,
			col : {r : 0, b : 0, g : 0},
			angVel : 0,
			grav : 0,
			wind : 0,
			life : 0,
			pps : pps || 10,
			ttne : 0,
			
			init : function(src, color) {
				this.image.load(src);
				this.image = CGL.image.multiply(this.image, color);
			},
			
			update : function(delta) {
				this.ttne -= delta;
				if(this.ttne < 0)
				{
					this.addParticle();
					this.ttne = 1000/pps;
				}
				for(var i = 0; i < this.particles.length; i++)
				{
					if(this.particles[i].update(this.grav, this.wind, this.growth))
					{
						this.particles.splice(i, 1);
					}
				}
			},
			
			render : function(ctx) {
				for(var i = 0; i < this.particles.length; i++)
				{	
					this.particles[i].render(this.image, ctx);	
				}
			},
			
			setPos : function(srcx, srcy) {
				this.pos.x = srcx;
				this.pos.y = srcy;
			},
			
			setMaxVel : function(srcvx, srcvy) {
				this.velMax.x = srcvx;
				this.velMax.y = srcvy;
			},
			
			setMinVel : function(srcvx, srcvy) {
				this.velMin.x = srcvx;
				this.velMin.y = srcvy;
			},
			
			setGravity : function(srcg) {
				this.grav = srcg;
			},
			
			setWind : function(srcw) {
				this.wind = srcw;
			},
			
			setLife : function(srcl) {
				this.life = srcl;
			},
			
			setInitSize : function(srcmin, srcmax) {
				this.initSize.min = srcmin;
				this.initSize.max = srcmax;
			},
			
			setGrowth : function(srcg) {
				this.growth = srcg;
			},
			
			addParticle : function() {
				var tempParticle = new CGL.particle.Particle(this.pos.x - this.image.image.width/2,
						this.pos.y - this.image.image.height/2,
						this.genValue(this.velMin.x, this.velMax.x),
						this.genValue(this.velMin.y, this.velMax.y),
						this.genValue(this.initSize.min, this.initSize.max),
						this.life);
				this.particles.push(tempParticle);
			},
			
			genValue : function(min, max) {
				var value;
				value = Math.random() * (max - min);
				return value + min;
			}
		};
		return emit;
	},

	System : function() {
		sys = {
			emitters : new Array(),
			
			addSystem : function(emitter) {
				this.emitters.push(emitter);
			},
			
			update : function(delta) {
				for(var i = 0; i < this.emitters.length; i++)
				{
					this.emitters[i].update(delta);
				}
			},
			
			render : function(ctx) {
				for(var i = 0; i < this.emitters.length; i++)
				{
					this.emitters[i].render(ctx);
				}
			}
		};
		return sys;
	}
};